/*
 * Copyright 2007-2008 Andrew O'Malley
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package ws.quokka.plugin.javadoc;

import org.apache.tools.ant.taskdefs.Javadoc;
import org.apache.tools.ant.types.Path;

import ws.quokka.core.plugin_spi.support.AbstractPlugin;
import ws.quokka.core.plugin_spi.support.ResourcesList;
import ws.quokka.core.plugin_spi.support.Setter;

import ws.quokka.plugin.devreport.DevReportHelper;

import java.util.Iterator;


/**
 *
 */
public class JavadocPlugin extends AbstractPlugin {
    //~ Static fields/initializers -------------------------------------------------------------------------------------

    private static final String CLASSPATH = "classpath";

    //~ Instance fields ------------------------------------------------------------------------------------------------

    private DevReportHelper helper;

    //~ Methods --------------------------------------------------------------------------------------------------------

    public void initialise() {
        super.initialise();
        helper = new DevReportHelper(getResources(), properties(), log());
    }

    public void collect() {
        helper.addResource(properties().getPath("sourcePath"));
        helper.addResource(CLASSPATH, getResources().getPathGroupAsPath("compile"));

        if (properties().getBoolean("propogate")) {
            propogate();
        }
    }

    public void propogate() {
        helper.propogateResources();
        helper.propogateResources(CLASSPATH);
    }

    public void report() {
        helper.addReport();
        report(helper.getLocalResources(), helper.getLocalResources(CLASSPATH));
    }

    public void aggregateReport() {
        helper.addReport();
        report(helper.getAggregatedResources(), helper.getAggregatedResources(CLASSPATH));
    }

    public void report(ResourcesList sourcePaths, ResourcesList classpaths) {
        Javadoc javadoc = (Javadoc)utils().init(new Javadoc(), "javadoc");
        javadoc.setClasspath(mergePaths(classpaths));
        javadoc.setSourcepath(mergePaths(sourcePaths));

        String[] attributes = new String[] {
                "destDir", "maxMemory", "packageNames", "packageList", "bootClassPath", "bootClassPathRef", "extDirs",
                "overview", "access", "public", "protected", "package", "private", "old", "verbose", "locale",
                "encoding", "version", "use", "author", "splitIndex", "windowTitle", "docTitle", "header", "footer",
                "bottom", "link", "linkOffline", "group", "noDeprecated", "noDeprecatedlist", "noTree", "noIndex",
                "noHelp", "noNavBar", "serialWarn", "helpFile", "stylesheetFile", "charset", "docEncoding", "doclet",
                "docletPath", "docletPathRef", "additionalParam", "failOnError", "excludePackageNames",
                "defaultExcludes", "useExternalFile", "source", "linkSource", "breakIterator", "noQualifier",
                "includeNoSourcePackages", "executable"
            };
        Setter setter = new Setter(properties());
        setter.set(javadoc, attributes);
        javadoc.perform();
    }

    private Path mergePaths(ResourcesList paths) {
        Path merged = new Path(getProject());

        for (Iterator i = paths.getNested().iterator(); i.hasNext();) {
            Path path = (Path)i.next();
            merged.add(path);
        }

        return merged;
    }
}
